/*
 * GroupDaoImpl.java
 *
 * Created on September 25, 2008, 4:32 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.vacuela.publishmx.dal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.vacuela.publishmx.domains.Group;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author nestor.carrasco
 */
public class GroupDaoImpl extends SqlMapClientDaoSupport  implements GroupDaoIfc{
    
    /** Creates a new instance of GroupDaoImpl */
    public GroupDaoImpl() {
    }
    @Transactional
    public boolean saveUpdate(Group group) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        if (group.getId() == null){
            template.insert("insertGroup", group);
            template.insert("insertMyGroupId", group);
        }else{
            template.update("updateGroup", group);
        }
        return true;
    }
    @Transactional(readOnly=true)
    public    List<Group>  getGroupByUserId(Integer userId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        List<Group> objectGroup = (List<Group>) template.queryForList("selectGroupByUserId", userId);
        return objectGroup;
    }
    @Transactional(readOnly=true)
    public List<Group> getGroupList() {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        return (List<Group>)template.queryForList("selectAllGroups");
    }
    @Transactional(readOnly=true)
    public Group getGroup(Group group) {
         SqlMapClientTemplate template = getSqlMapClientTemplate();
        Group objectGroup = (Group) template.queryForObject("selectGroupById", group);
        return objectGroup;
    }
    @Transactional(readOnly=true)
    public List<Group> findGroupByName(String name) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Map mapParam = new HashMap();
        mapParam.put("criteria",name+"%");
        return (List<Group>)template.queryForList("findGroupsByName",mapParam);
    }
    @Transactional
    public boolean deleteGroup(Integer groupId) {
         SqlMapClientTemplate template = getSqlMapClientTemplate();         
        template.delete("deleteGroupFromMyGroup", groupId);
        template.delete("deleteGroup", groupId);
        return true;
    }   
}
